<div id="addWarehouserStorageFormContent" style="height: 100%">
  <form class="layui-form open-form" id="addCustomerProductForm" lay-filter="addCustomerProductForm">
    <div class="open-model">
      <div class="open-auto">
        <div class="open-details-title" style="margin-bottom: 20px">入库库位信息</div>
        <table class="layui-table">
          <thead>
          <tr>
            <th lay-data="{field:'shipmentOrderCode', width:150, sort: true, fixed: 'left'}">
              捆包号
            </th>
            <th lay-data="{field:'shipmentOrderCode', width:150, sort: true, fixed: 'left'}">
              规格号
            </th>
            <th lay-data="{field:'materialName', width:150, sort: true, fixed: 'left'}">产地/厂家
            </th>
            <th lay-data="{field:'materialSpec', width:150, sort: true, fixed: 'left'}">品种</th>
            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">牌号</th>
            <th lay-data="{field:'materialSpec', width:150, sort: true, fixed: 'left'}">
              所属客户
            </th>
            <th lay-data="{field:'materialSpec', width:150, sort: true, fixed: 'left'}">入库数量</th>
            <th lay-data="{field:'materialSpec', width:150, sort: true, fixed: 'left'}">
              毛重
            </th>
            <th lay-data="{field:'materialSpec', width:150, sort: true, fixed: 'left'}">净重</th>
            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">签收时间</th>
            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">入库仓库</th>
            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">仓库性质</th>
            <!--              <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">描述</th>-->
          </tr>
          </thead>
          <tbody>
          <tr>
            <td>
              <input class="layui-input adv-disable" id="userBundleCode" disabled
                     name="userBundleCode"/>
            </td>
            <td>
              <input type="text" class="layui-input" disabled id="specifications"
                     name="specifications"/>
            </td>
            <td>
              <input type="text" class="layui-input" disabled id="productProducer"
                     name="productProducer"/>
            </td>
            <td>
              <input class="layui-input adv-disable" id="productVariety" disabled
                     name="productVariety"/>
            </td>
            <td>
              <input class="layui-input adv-disable" id="productMark" disabled
                     name="productMark"/>
            </td>
            <td>
              <input class="layui-input adv-disable" id="customerName" disabled
                     name="customerName"/>
            </td>

            <td>
              <input type="text" class="layui-input" disabled name="quality"/>
            </td>

            <td>
              <input type="text" class="layui-input" disabled name="grossWeight"/>
            </td>
            <td>
              <input type="text" class="layui-input" disabled name="netWeight"/>
            </td>
            <td>
              <input type="text" class="layui-input" disabled name="createTime"/>
            </td>
            <td>
              <input class="layui-input adv-disable" id="warehouseFactoryAdministrationName" disabled name="warehouseFactoryAdministrationName"/>
            </td>
            <td>
              <input class="layui-input adv-disable" id="warehouseFeatureTypeDesc" disabled name="warehouseFeatureTypeDesc"/>
            </td>
            <!--              <td>-->
            <!--                <input class="layui-input adv-disable" id="creatorName" disabled-->
            <!--                       name="creatorName"/>-->
            <!--              </td>-->
          </tr>
          </tbody>
        </table>
        <div class="open-details-title" style="margin-bottom: 20px">入库物料信息</div>
        <table class="layui-table" id="customerProductsTable" lay-filter="customerProductsTable"></table>
<!--        <div class="open-details-title" style="margin-bottom: 20px">入库作业</div>-->
<!--        &lt;!&ndash;        <input name="id" disabled&ndash;&gt;-->
<!--        &lt;!&ndash;               class="layui-input" type="hidden">&ndash;&gt;-->
<!--        <input type="text" name="warehouseOrderId" hidden />-->
<!--        <input type="text" name="signInRecordId" hidden />-->
<!--        <input type="text" name="warehouseFactoryShelfSpaceId" hidden />-->
<!--        <input type="text" name="warehouseAreaId" hidden />-->
<!--        <div class="layui-col-md3">-->
<!--          <div class="layui-form-item">-->
<!--            <label class="layui-form-label">产品SKU编号</label>-->
<!--            <input type="text" name="warehouseProductId" hidden />-->
<!--            <div class="layui-input-inline">-->
<!--              <input name="warehouseProductName" type="text" class="layui-input" disabled class="layui-input" />-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->
<!--        <div class="layui-col-md3">-->
<!--          <div class="layui-form-item">-->
<!--            <label class="layui-form-label">规格号</label>-->
<!--            <div class="layui-input-inline">-->
<!--              <input type="text" name="productSpecCode" disabled class="layui-input" />-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->

<!--        <div class="layui-col-md3">-->
<!--          <div class="layui-form-item">-->
<!--            <label class="layui-form-label">产地/厂家</label>-->
<!--            <div class="layui-input-inline">-->
<!--              <input type="text" name="productProducer" disabled class="layui-input" />-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->

<!--        <div class="layui-col-md3">-->
<!--          <div class="layui-form-item">-->
<!--            <label class="layui-form-label">品种</label>-->
<!--            <div class="layui-input-inline">-->
<!--              <input type="text" name="productVarietyName" disabled class="layui-input" />-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->

<!--        <div class="layui-col-md3">-->
<!--          <div class="layui-form-item">-->
<!--            <label class="layui-form-label">牌号</label>-->
<!--            <div class="layui-input-inline">-->
<!--              <input type="text" name="productMarkName" disabled class="layui-input" />-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->
<!--        <div class="layui-col-md3">-->
<!--          <div class="layui-form-item">-->
<!--            <label class="layui-form-label">入库仓库</label>-->
<!--            <div class="layui-input-inline">-->
<!--              <input type="text" name="warehouseFactoryAdministrationName" disabled class="layui-input" />-->
<!--              <input type="text" name="warehouseFactoryAdministrationId" hidden />-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->
<!--        <div class="layui-col-md3">-->
<!--          <div class="layui-form-item">-->
<!--            <label class="layui-form-label">仓库性质</label>-->
<!--            <div class="layui-input-inline">-->
<!--              <input type="text" name="productVarietyName" disabled class="layui-input" />-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->
<!--        <div class="layui-col-md3">-->
<!--          <div class="layui-form-item">-->
<!--            <label class="layui-form-label">所属客户</label>-->
<!--            <input type="text" name="customerId" hidden />-->
<!--            <div class="layui-input-inline">-->
<!--              <input type="text" name="customer" disabled class="layui-input" />-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->

<!--        <div class="layui-col-md3">-->
<!--          <div class="layui-form-item">-->
<!--            <label class="layui-form-label">入库数量</label>-->
<!--            <div class="layui-input-inline">-->
<!--              <input type="text" name="inventoryQuantity" disabled class="layui-input" />-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->
<!--        <div class="layui-col-md3">-->
<!--          <div class="layui-form-item">-->
<!--            <label class="layui-form-label">总净重</label>-->
<!--            <div class="layui-input-inline">-->
<!--              <input type="text" name="netWeight" disabled class="layui-input" />-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->
<!--        <div class="layui-col-md3">-->
<!--          <div class="layui-form-item">-->
<!--            <label class="layui-form-label">总毛重</label>-->
<!--            <div class="layui-input-inline">-->
<!--              <input type="text" name="grossWeight" disabled class="layui-input" />-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->
<!--        <div class="open-details-title" style="margin-bottom: 5px">入库信息</div>-->
<!--        &lt;!&ndash; {{# if(d.eventName!="detail"){ }} &ndash;&gt;-->
<!--        <button class="layui-btn" id="addTableData" style="margin-top: 15px">添加入库作业物资</button>-->
<!--        &lt;!&ndash; {{# } }} &ndash;&gt;-->

<!--        <table class="layui-table" id="customerProductsTable" lay-filter="customerProductsTable"></table>-->
      </div>

      <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        <!-- 客服代表, 客户物料名称, 客户物料编码 -->
        <!-- 提交按钮 -->
        <!-- {{# if(d.eventName!='detail'){ }} -->
        <button class="layui-btn" lay-filter="addCustomerProductBtn" type="button" lay-submit>保存</button>
        <!-- {{# } }} -->
      </div>
    </div>
  </form>
</div>

<script type="text/html" id="barterTableBar">
  <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>
  <!--    <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="detail">详情</button>-->
</script>
<script>
  layui.use(["form", "table", "laydate", "layedit", "laydate"], async function () {
    $ = layui.jquery;
    var form = layui.form;
    var admin = layui.admin;
    var table = layui.table;
    var upload = layui.upload;
    var config = layui.config;
    var laytpl = layui.laytpl;
    let nowData = { ...parentData };
    let data = nowData.data;
    let productTypeList = [];
    // const findFactoryShelfSpaceByWarehouseAreaIdList=[]
    const warehouseShelfSpaceIdList = await admin.reqPro('warehouse/warehouse/admin/administration/findFactoryShelfSpaceByWarehouseAreaId', {id:data.warehouseAreaId,limit: 99999,});
    const findFactoryShelfSpaceByWarehouseAreaIdList=warehouseShelfSpaceIdList.data
    // admin.req(
    //   "enterprise/product/admin/productType/list",
    //   {
    //     nowData
    //   },
    //   function (res) {
    //     productTypeList = res.data;
    //     table.reload("customerProductsTable");
    //   }
    // );
    const tableVal =table.render({
      elem: "#customerProductsTable",
      page: false,
      height: 100,
      // data: [],
      cols: [
        [
          {
            field: "code",
            title: "所属库区",
            align: "center",

          },{
          field: "warehouseShelfSpaceId",
          title: "入库库位",
          templet: admin.setTableSelect("warehouseShelfSpaceId",
                  findFactoryShelfSpaceByWarehouseAreaIdList.map((v) => {
                    return {
                      label: v.code,
                      value: v.id
                    };
                  }), false, "warehouseShelfSpaceId")
        },
          {
            field: "columnNum",
            title: "所属层",
            align: "center",
          },
          {
            field: "layerNum",
            title: "所属列",
            align: "center",
          },
          {
            field: "warehouseShelfSpaceName",
            title: "所属货架",
            align: "center",
          },
        ]
      ],
      done: function (e) {
        admin.tableSelectChange("customerProductsTable", "warehouseShelfSpaceId",async (index, value,tableData) => {
          if (value) {
            let kDATA=await admin.reqPro('warehouse/warehouse/admin/administration/findFactoryShelfByShelId', {id:value})
            let obj=null
             // obj=kDATA.data
            if(kDATA.data==null){
              obj={}
            }else{
              obj=kDATA.data
            }
            obj.code=data.code
            obj.warehouseAreaId=data.warehouseAreaId
            obj.warehouseShelfSpaceId=value
            obj.warehouseShelfId=obj.id
            if(obj.columnNum===''){
              obj.columnNum='-'
            }
            if(obj.layerNum===''){
              obj.layerNum='-'
            }
            if(obj.name===''){
              obj.warehouseShelfSpaceName='-'
            }else{
              obj.warehouseShelfSpaceName=obj.name
            }
            console.log(obj)
            tableVal.reload({
              data:[obj]
            });
            // console.log(obj,"11111111")
          } else {

          }
        });
      }
    });
    console.log("nowData", nowData);

    form.val("addCustomerProductForm", { ...nowData.data, ...nowData.formData });
    if(data&&data.warehouseAreaId){
      // data.columnNum=''
      // data.layerNum=''
      // data.name=''
      // table.reload("customerProductsTable", {
      //   data: [item]
      // });
      tableVal.reload({
        data: [data]
      });
    }


    // const res = await admin.reqPro("warehouse/warehouse/product/selectProductDetail?productStatus=2", { warehouseProductId: nowData.data.warehouseProductId });
    // 筛选出状态为可选的条码
    //1、发货 2、签收 3、入库 4、出库
    // const BundleList = res.data;
    // const BundleList = res.data.filter((v) => v.productStatus == 2);


    // 监听删除按钮点击事件
    table.on("tool(customerProductsTable)", function (obj) {
      var data = obj.data; // 获得当前行数据
      if (obj.event === "del") {
        event.preventDefault(); // 阻止默认行为
        layer.confirm("确定删除该条数据？", function (index) {
          // 获取当前行的索引
          var rowIndex = $(obj.tr).data("index");

          // 从缓存中删除对应的数据
          let tableData = table.cache["customerProductsTable"];
          tableData.splice(rowIndex, 1); // 根据索引删除数据

          // 更新表格
          table.reload("customerProductsTable", { data: tableData });

          layer.close(index);

          // 重新计算入库数量、净重、毛重
          form.val("addCustomerProductForm", {
            inventoryQuantity: tableData.filter((v) => v.sysBundleCode).length,
            netWeight: tableData.reduce((a, b) => a + Number(b.netWeight || 0), 0),
            grossWeight: tableData.reduce((a, b) => a + Number(b.grossWeight || 0), 0)
          });
        });
      }
    });

    // $("#addWarehouserStorageFormContent .layui-table-body").css("overflow", "visible");
    // $("#addWarehouserStorageFormContent .layui-table-box").css("overflow", "visible");
    // $("#addWarehouserStorageFormContent .layui-table-view").css("overflow", "visible");
    admin.tableSelectChange("customerProductsTable", "userBundleCode", (index, value, tableData, field) => {
      let obj = BundleList.find((v) => v.userBundleCode == value);

      obj = {
        sysBundleCode: obj.sysBundleCode,
        userBundleCode: obj.userBundleCode,
        unit: obj.unit,
        grossWeight: obj.grossWeight,
        netWeight: obj.netWeight,
        proUnitId: obj.proUnitId,
        id: obj.id
      };

      tableData[index] = obj;
      table.reload("customerProductsTable", {
        data: tableData
      });

      form.val("addCustomerProductForm", {
        inventoryQuantity: tableData.filter((v) => v.sysBundleCode).length,
        netWeight: tableData.reduce((a, b) => a + Number(b.netWeight || 0), 0),
        grossWeight: tableData.reduce((a, b) => a + Number(b.grossWeight || 0), 0)
      });
    });

    $("#addTableData").click(function () {
      event.preventDefault(); // 明确阻止默认行为
      let list = table.cache["customerProductsTable"];
      list.unshift({
        sysBundleCode: "",
        userBundleCode: "",
        unit: "",
        grossWeight: "",
        netWeight: "",
        proUnitId: "",
        id: ""
      });
      table.reload("customerProductsTable", {
        data: list
      });
      return false;
    });

    // 监听提交
    form.on("submit(addCustomerProductBtn)", function (d) {
      const formData = { ...d.field };
      let list = table.cache["customerProductsTable"];
      console.log(list,"list",formData)
      if (list[0].warehouseShelfSpaceId ==='') {
        layer.msg("请选择入库库位");
        return true;
      }
      childData.list = list.map((v) => {
        // v.warehouseShelfSpaceId = v.warehouseShelfSpaceId;
        // v.warehousePrice = v.price;
        delete v.id;
        return v;
      });
      layer.close(parentData.zindex)
    });
  });
</script>

<style>
  #addWarehouserStorageFormContent .layui-form-label {
    width: 100px;
  }
  #addWarehouserStorageFormContent .layui-table td, #addWarehouserStorageFormContent .layui-table th{
    padding: 9px 10px;
  }
  #addWarehouserStorageFormContent .layui-table-view .layui-table td, #addWarehouserStorageFormContent .layui-table-view .layui-table th{
    padding: 10px 0;
  }

  #addWarehouserStorageFormContent .layui-form-item .layui-input-inline {
    width: 170px;
  }

  #addWarehouserStorageFormContent .layui-table-cell {
    overflow: visible !important;
  }
</style>